﻿
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.Entity;
using System.ComponentModel.DataAnnotations;
using System.Globalization;
using System.Web.Mvc;
using System.Web.Security;
using System.Data.Linq;
using projectManagementWeb.Validations;


namespace projectManagementWeb.Models.Manager
{
    [ProjectClassValidationAttribute]
    public class modifyProjectModel 
    {
        projectmanagementDBDataContext _dbContext = null;
        public modifyProjectModel()
        {
            _dbContext = new projectmanagementDBDataContext();
        }

        [Required]
        public int ID { get; set; }

        [Required(ErrorMessage="El {0} es requerido")]
        [StringLength(100, ErrorMessage = "El {0} debe contener por lo menos un carater y ser menor a 100 caracteres.", MinimumLength = 1)]
        [Display(Name = "Nombre del Proyecto")]
        public string Name { get; set; }


        [Required(ErrorMessage = "La {0} es requerido")]
        [StringLength(100, ErrorMessage = "La {0} debe contener por lo menos un carater y ser menor a 100 caracteres.", MinimumLength = 1)]
        [Display(Name = "Descripción del Proyecto")]
        public string Description { get; set; }

        [Required(ErrorMessage = "La {0} es requerido")]
        //[DataType(DataType.DateTime)]
        [Display(Name = "Fecha de Inicio del Proyecto")]
        public DateTime startDate { get; set; }

        [Required(ErrorMessage = "La {0} es requerido")]
        [Display(Name = "Fecha de Finalización del Proyecto")]
        public DateTime endDate { get; set; }

        //[Required(ErrorMessage = "El {0} es requerido")]
        //[Range(1, int.MaxValue)]
        [Display(Name = "Líder de Proyecto")]
        public int projectLeaderID { get; set; }

        [Required]
        [Range(1, int.MaxValue)]
        [Display(Name = "Patrocinador")]
        public int sponsorID { get; set; }

        [Required]
        [Range(1, int.MaxValue)]
        [Display(Name = "Estatus")]
        public int statusID { get; set; }

        public int userID { get; set; }



        public IEnumerable<SelectListItem> ProjectLeaderIDList
        {
            get
            {
                List<SelectListItem> projectLeaderIDList = null;

                projectLeaderIDList = _dbContext.spGetProyectLeaders()
                .Select(i => new SelectListItem
                {
                    Value = i.id.ToString(),
                    Text = i.UserName.ToString()
                }).ToList();
                //Add current project leader
                try
                {
                    if (ID != null && ID > 1)
                    {
                        ISingleResult<spGetProjectByIDResult> project = _dbContext.spGetProjectByID(ID);
                        ISingleResult<spGetUserByIDResult> user = _dbContext.spGetUserByID(project.First().projectLeaderID);
                        projectLeaderIDList.Add(new SelectListItem
                        {
                            Value = user.First().id.ToString(),
                            Text = user.First().UserName.ToString()
                        });
                    }
                }
                catch (Exception) { }

                if (projectLeaderIDList == null || projectLeaderIDList.Count() == 0)
                {
                    List<SelectListItem> list = new List<SelectListItem>();
                    list.Add(new SelectListItem
                    {
                        Value = "-1",
                        Text = "No tiene líderes de proyecto válidos"
                    });
                    return list;
                }
                return projectLeaderIDList;
            }
        }

        public IEnumerable<SelectListItem> SponsorIDList
        {
            get
            {
                IEnumerable<SelectListItem> sponsorIDList = null;
                sponsorIDList = _dbContext.spGetManagers()
                .Select(i => new SelectListItem
                {
                    Value = i.id.ToString(),
                    Text = i.UserName.ToString()
                }).ToList();

                if (sponsorIDList == null || sponsorIDList.Count() == 0)
                {
                    List<SelectListItem> list = new List<SelectListItem>();
                    list.Add(new SelectListItem
                    {
                        Value = "-1",
                        Text = "No tiene patrocinadores válidos"
                    });
                    return list;
                }
                return sponsorIDList;
            }
        }

        public IEnumerable<SelectListItem> StatusIDList
        {
            get
            {
                IEnumerable<SelectListItem> statusIDList = null;
                statusIDList = _dbContext.spGetStatusTypes()
               .Select(i => new SelectListItem
               {
                   Value = i.ID.ToString(),
                   Text = i.name.ToString()
               }).ToList();

                if (statusIDList == null || statusIDList.Count() == 0)
                {
                    List<SelectListItem> list = new List<SelectListItem>();
                    list.Add(new SelectListItem
                    {
                        Value = "-1",
                        Text = "No tiene estatus válidos"
                    });
                    return list;
                }
                return statusIDList;
            }
        }
        public IEnumerable<SelectListItem> ProjectList
        {
            get
            {
                IEnumerable<SelectListItem> projectList = null;
                projectList = _dbContext.spGetAllProjects()
               .Select(i => new SelectListItem
               {
                   Value = i.ID.ToString(),
                   Text = i.name.ToString()
               }).ToList();

                if (projectList == null || projectList.Count() == 0)
                {
                    List<SelectListItem> list = new List<SelectListItem>();
                    list.Add(new SelectListItem
                    {
                        Value = "-1",
                        Text = "No hay proyectos disponibles"
                    });
                    return list;
                }
                return projectList;
            }
        }

    }
}
